package patient

import (
	"github.com/gin-gonic/gin"
	"hosptial-record/api/base"
	"hosptial-record/forms"
	"hosptial-record/global"
	"hosptial-record/global/response"
	"hosptial-record/model"
	"net/http"
)

// List 患者列表
func List(ctx *gin.Context) {
	var form forms.GetPatientForm
	if err := ctx.ShouldBindQuery(&form); err != nil {
		base.HandleValidatorError(ctx, err)
		return
	}
	db := global.DB
	if form.Name != "" {
		db = db.Where("name LIKE ?", "%"+form.Name+"%")
	}
	if form.CardNumber != "" {
		db = db.Where("card_number = ?", form.CardNumber)
	}
	if form.PatientNumber != "" {
		db = db.Where("patient_number = ?", form.PatientNumber)
	}
	var patients []model.Patient
	var total int64
	res := db.
		Model(&model.Patient{}).
		Count(&total).
		Order("id DESC").
		Scopes(base.Paginate(int(form.Page), int(form.PageSize))).
		Find(&patients)
	if res.Error != nil {
		ctx.JSON(http.StatusBadGateway, response.BaseResponse{
			Msg:  "获取患者失败",
			Data: res.Error,
		})
		return
	}
	ctx.JSON(http.StatusOK, response.ListResponse{
		Msg: "获取成功",
		Data: response.ListData{
			Data:  patients,
			Total: total,
		},
	})
}

// Detail 患者详情
func Detail(ctx *gin.Context) {
	id, err := base.GetID2Param(ctx)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, response.BaseResponse{
			Msg: "患者ID不合法",
		})
		return
	}
	var patient model.Patient
	patient.ID = id
	if res := global.DB.First(&patient); res.Error != nil {
		ctx.JSON(http.StatusBadGateway, response.BaseResponse{
			Msg:  "获取患者信息失败",
			Data: res.Error,
		})
		return
	}
	ctx.JSON(http.StatusOK, response.BaseResponse{
		Msg:  "获取成功",
		Data: patient,
	})
}

// Create 创建患者
func Create(ctx *gin.Context) {
	var form forms.PatientForm
	if err := ctx.ShouldBind(&form); err != nil {
		base.HandleValidatorError(ctx, err)
		return
	}
	patient := model.Patient{
		Name:          form.Name,
		Age:           form.Age,
		PatientNumber: form.PatientNumber,
		CardNumber:    form.CardNumber,
		Gender:        form.Gender,
		Position:      form.Position,
	}
	res := global.DB.Create(&patient)
	if res.Error != nil {
		ctx.JSON(http.StatusBadGateway, response.BaseResponse{
			Msg:  "创建失败",
			Data: res.Error,
		})
		return
	}
	ctx.JSON(http.StatusOK, response.BaseResponse{
		Msg: "创建成功",
	})
}

// Delete 删除患者
func Delete(ctx *gin.Context) {
	id, err := base.GetID2Param(ctx)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, response.BaseResponse{
			Msg: "患者ID不合法",
		})
		return
	}
	// 判断是否存在
	if res := global.DB.First(&model.Patient{}, id); res.RowsAffected == 0 {
		ctx.JSON(http.StatusBadRequest, response.BaseResponse{
			Msg: "患者不存在",
		})
		return
	}
	res := global.DB.Delete(&model.Patient{}, id)
	if res.Error != nil {
		ctx.JSON(http.StatusBadGateway, response.BaseResponse{
			Msg:  "删除失败",
			Data: res.Error,
		})
		return
	}
	ctx.JSON(http.StatusOK, response.BaseResponse{
		Msg: "删除成功",
	})
}

// Update 更新患者
func Update(ctx *gin.Context) {
	var form forms.EditPatientForm
	if err := ctx.ShouldBind(&form); err != nil {
		base.HandleValidatorError(ctx, err)
		return
	}
	// 判断是否存在
	if res := global.DB.First(&model.Patient{}, form.ID); res.RowsAffected == 0 {
		ctx.JSON(http.StatusBadRequest, response.BaseResponse{
			Msg: "患者不存在",
		})
		return
	}
	patient := model.Patient{
		Name:          form.Name,
		Age:           form.Age,
		PatientNumber: form.PatientNumber,
		CardNumber:    form.CardNumber,
		Gender:        form.Gender,
		Position:      form.Position,
	}
	patient.ID = form.ID
	res := global.DB.Updates(&patient)
	if res.Error != nil {
		ctx.JSON(http.StatusBadGateway, response.BaseResponse{
			Msg:  "更新失败",
			Data: res.Error,
		})
		return
	}
	ctx.JSON(http.StatusOK, response.BaseResponse{
		Msg: "更新成功",
	})
}
